package com.wangcang.widgetlibrary.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表B樣式
 * Created by ShineF on 2017/6/22 0022.
 */

public class BListActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mIvBack;
    private ListView mLvContent;
    private List<String> mStrList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_b);
        init();
    }

    public void init() {
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mLvContent = (ListView) findViewById(R.id.lv_list_a);
        mIvBack.setOnClickListener(this);
        initData();
    }

    public void initData() {
        mStrList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mStrList.add("這裏是一個列表名稱.");
        }
        MyAdapter adapter = new MyAdapter();
        mLvContent.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    class MyAdapter extends BaseAdapter {

        ViewHolder mHolder;

        @Override
        public int getCount() {
            return mStrList.size();
        }

        @Override
        public Object getItem(int position) {
            return mStrList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(BListActivity.this, R.layout.view_list_a_layout, null);
                mHolder = new ViewHolder(convertView);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ViewHolder) convertView.getTag();
            }
            mHolder.mTv.setText(mStrList.get(position));
            return convertView;
        }

        class ViewHolder {
            private TextView mTv;

            public ViewHolder(View view) {
                mTv = (TextView) view.findViewById(R.id.tv_text);
            }
        }
    }
}
